<template>
  <div class="bodyBox">
    <FrameStyle>
      <!-- 面包屑 -->
      <Breadcrumb :breadName="`志愿者管理`" slot="breadcrumb" />
      <!-- 搜索框 -->
      <el-input slot="search">
        <el-button class="el-icon-search" slot="append"></el-button>
      </el-input>
      <!-- 添加按钮 -->
      <el-button type="primary" slot="addBtn" @click="addDiaLogVisible = true"
        >添加志愿者</el-button
      >
      <!-- 表格列表 -->
      <el-table :data="tableData" stripe slot="tableList" height="370px">
        <el-table-column label="创建时间" prop="createTime" width="120px">
        </el-table-column>
        <el-table-column
          prop="position"
          label="标题"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column prop="workRequire" label="招聘要求"></el-table-column>
        <el-table-column label="工资待遇" prop="workSalary" width="120px">
        </el-table-column>
        <el-table-column prop="recruitsNum" label="招聘人数" width="120px">
        </el-table-column>
        <el-table-column prop="lead" label="负责人" width="100px">
        </el-table-column>
        <el-table-column prop="picture" label="负责人" width="100px">
          <template slot-scope="scope">
            <img :src="scope.row.picture" width="80px" height="50px" />
          </template>
        </el-table-column>
        <el-table-column
          prop="contact"
          label="联系方式"
          width="120px"
        ></el-table-column>
        <el-table-column label="点击次数" prop="clickNum" width="100px">
        </el-table-column>
        <el-table-column label="操作" width="150px">
          <template slot-scope="scope">
            <el-button size="mini" @click="openChangeDiaLog(scope.row)"
              >修改</el-button
            >
            <el-button
              type="danger"
              size="mini"
              @click="delVolunteer(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <Pagination
        slot="pageSlot"
        :pageNo="pageNum"
        :pageSize="pageSize"
        :totalRows="totalRows"
        @pageChange="pageChange"
      />
    </FrameStyle>
    <el-dialog :visible.sync="addDiaLogVisible">
      <el-form :model="addForm">
        <el-form-item label="职位">
          <el-input v-model="addForm.position"></el-input>
        </el-form-item>
        <el-form-item label="招聘要求">
          <el-input v-model="addForm.workRequire"></el-input>
        </el-form-item>
        <el-form-item label="工资待遇">
          <el-input v-model="addForm.workSalary"></el-input>
        </el-form-item>
        <el-form-item label="工作地点">
          <el-input v-model="addForm.workPlace"></el-input>
        </el-form-item>
        <el-form-item label="工作时间">
          <el-input v-model="addForm.workTime"></el-input>
        </el-form-item>
        <el-form-item label="招聘人数">
          <el-input v-model="addForm.recruitsNum"></el-input>
        </el-form-item>
        <el-form-item label="负责人">
          <el-input v-model="addForm.lead"></el-input>
        </el-form-item>
        <el-form-item label="联系方式">
          <el-input v-model="addForm.contact"></el-input>
        </el-form-item>
        <el-form-item label="图片">
          <UploadImg :type="`VOLUNTEER`" @getImgUrl="getImgUrl" />
        </el-form-item>
      </el-form>
      <div>
        <el-button type="primary" @click="addVolunteer">确认</el-button>
        <el-button>重置</el-button>
      </div>
    </el-dialog>
    <el-dialog :visible.sync="changeDiaLogVisible">
      <el-form :model="changeForm" ref="ruleForm">
        <el-form-item label="职位" prop="position">
          <el-input v-model="changeForm.position"></el-input>
        </el-form-item>
        <el-form-item label="招聘要求" prop="workRequire">
          <el-input v-model="changeForm.workRequire"></el-input>
        </el-form-item>
        <el-form-item label="工资待遇" prop="workSalary">
          <el-input v-model="changeForm.workSalary"></el-input>
        </el-form-item>
        <el-form-item label="工作地点" prop="workPlace">
          <el-input v-model="changeForm.workPlace"></el-input>
        </el-form-item>
        <el-form-item label="工作时间" prop="workTime">
          <el-input v-model="changeForm.workTime"></el-input>
        </el-form-item>
        <el-form-item label="招聘人数" prop="recruitsNum">
          <el-input v-model="changeForm.recruitsNum"></el-input>
        </el-form-item>
        <el-form-item label="负责人" prop="lead">
          <el-input v-model="changeForm.lead"></el-input>
        </el-form-item>
        <el-form-item label="联系方式" prop="contact">
          <el-input v-model="changeForm.contact"></el-input>
        </el-form-item>
        <el-form-item label="图片">
          <UploadImg :type="`VOLUNTEER`" @getImgUrl="getImgUrl" />
        </el-form-item>
      </el-form>
      <div>
        <el-button type="primary" @click="changeVolunteer">确认</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  reqZhiYuanZeList,
  reqAddData,
  reqDelData,
  reqChangeData,
} from "../../../api";
export default {
  data() {
    return {
      tableData: [],
      pageNum: 1,
      pageSize: 10,
      totalRows: 1,
      imgUrl: "",
      addDiaLogVisible: false,
      changeDiaLogVisible: false,
      addForm: {
        position: "",
        work_require: "",
        work_salary: "",
        work_place: "",
        work_time: "",
        recruits_num: "",
        lead: "",
        contact: "",
        picture: "",
      },
      changeForm: {},
    };
  },
  created() {
    this.getData();
  },
  methods: {
    //获取志愿者列表数据
    async getData() {
      let { pageNum, pageSize } = this;
      let result = await reqZhiYuanZeList({ pageNum, pageSize });
      if (result.code != 200) return false;
      result.data.list.forEach((item) => {
        item.createTime = this.$format(item.createTime);
      });
      this.tableData = result.data.list;
      this.totalRows = result.data.total;
    },
    //添加志愿者
    async addVolunteer() {
      this.addForm.picture = this.imgUrl;
      let result = await reqAddData("/volunteer/add", this.addForm);
      if (result.code != 200) return this.$message.error("添加失败");
      this.$message({ type: "success", message: "添加成功" });
      this.getData();
      this.addDiaLogVisible = false;
    },
    //删除志愿者
    async delVolunteer(id) {
      let result = await reqDelData("/volunteer/delete", id);
      if (result.code != 200) return this.$message.error("删除失败");
      this.$message({ type: "success", message: "删除成功" });
      this.getData();
    },
    //打开修改弹出层
    openChangeDiaLog(row) {
      this.changeDiaLogVisible = true;
      delete row.createTime
      this.$nextTick(() => {
        this.changeForm = row;
      });
    },
    //修改志愿者
    async changeVolunteer() {
      this.imgUrl != "" ? (this.changeForm.picture = this.imgUrl) : false;
      let result = await reqChangeData("/volunteer/update", this.changeForm);
      if (result.code != 200) return this.$message.error("修改失败");
      this.$message({ type: "success", message: "修改成功" });
      this.getData();
      this.changeDiaLogVisible = false;
    },
    //重置表单
    resetForm(formName){
      this.$refs[formName].resetFields()
    },
    getImgUrl(url) {
      console.log(url);
      this.imgUrl = url;
    },
    pageChange(page) {
      this.pageNum = page;
      this.getData();
    },
  },
};
</script>